home *** CD-ROM | disk | FTP | other *** search
/ Especial Multimedia / Especial Multimedia.iso / Multimed / Fuentes / TTFPFX2.ZIP / TTFPFX2.C < prev    next >
C/C++ Source or Header  |  1997-09-14  |  5KB  |  164 lines

  1. /**************************************
  2. ttfpfx2.c   TTF (True Type Font)  Prefix Patch 
  3. ***************************************/
  4.  
  5. #include    <stdio.h> 
  6. #include    <ctype.h>     
  7. #include    <string.h>  
  8. #include    <stdlib.h>
  9. #include    <time.h>    
  10.  
  11. #include <memory.h>
  12. #include <io.h>
  13. #include <fcntl.h>
  14. #include <sys\types.h>
  15. #include <sys\stat.h>
  16. #include <direct.h>
  17. #include <dos.h> 
  18.  
  19. typedef int             BOOL ;  /* logical YES or NO         */
  20. typedef unsigned char   BYTE ;  /* binary file I/O           */
  21. typedef char*           STRN ;  /* STRing Null terminated    */
  22.  
  23. static FILE *inpfil ;        /* input prefix  =new=old=file1.TTF .. */ 
  24. static FILE *msgfil ;        /* diagnostic output file (inpfil).FX2 */ 
  25. static char  data[800] ;    /* buffer for old and new datums */ 
  26. static char *dap  ;        /* free-end ptr into data */
  27. static char *xold[8] ;         /* old ptrs into data */
  28. static char *xnew[8] ;         /* new ptrs into data */
  29. static int   xlen[8] ;         /* len of datums */ 
  30. static int   endix   ;        /* number of datums */ 
  31.  
  32. /*************************/
  33. static void patdoc(void)
  34. {
  35.  fprintf(stderr, " Single arg must be ascii input file name \n") ; 
  36.  fprintf(stderr, " Error... See TTFPFX2.DOC  \n" ) ; 
  37.  exit(1) ;  
  38. /****************************************/
  39. static FILE* jopen(char *nam, char *mode)
  40. {
  41.  FILE *hand = fopen(nam, mode) ; 
  42.  if(NULL != hand) return(hand) ;
  43.  fprintf(stderr," Unable to open file %s %s \n", nam,mode) ; 
  44.  patdoc() ; 
  45. }
  46. /********************************************/
  47. static void serfii( char *fmt, int i1, int i2) 
  48. {
  49.  fprintf(msgfil, fmt, i1,i2) ;  fputc( 10, msgfil) ; 
  50.  exit(2) ; 
  51. }
  52. /*****************************************/
  53. static void seeklog(int hand, long offlog)
  54. {
  55.  long log = lseek(hand, offlog, SEEK_SET) ; 
  56.  if(log == offlog) return ; 
  57.  serfii("lseek err %d%d",0,0) ; 
  58. }
  59. /***************************/
  60. static void repx(STRN filna)      /* file name to patch */
  61. {
  62.  #define MUL512  30720  
  63.  static char buf[MUL512+300] ; 
  64.  
  65.  char *bp, *end, c0 = *xold[0] ; 
  66.  long  offend, fillen , offlog = 0 ; 
  67.  int hand, was, buflen, xx ,  knt = 0 ; 
  68.  
  69.  hand = open(filna, O_BINARY|O_RDWR) ; 
  70.  if(-1 == hand) 
  71.  {      fprintf(msgfil," Error opening %s \n", filna) ; 
  72.      return ; 
  73.  }
  74.  fillen = filelength(hand) ; 
  75.  for( ; offlog<fillen ; offlog += MUL512 ) 
  76.  {  seeklog(hand, offlog) ; 
  77.     offend = offlog + MUL512 + xlen[1] ;
  78.     if(fillen<offend)  offend = fillen ; 
  79.     buflen = offend - offlog ; 
  80.     was = read(hand, buf, buflen) ;  
  81.     if(was != buflen) serfii("%d=read(%d)", was, buflen) ; 
  82.     was = knt ; bp=buf ; end = buf + buflen - xlen[0] ; 
  83.     for( ; bp<=end ; ++bp ) 
  84.       if(c0==*bp) 
  85.         for( xx=0 ; xx<endix ; ++xx) 
  86.            if(!memcmp(bp,xold[xx],xlen[xx]))  
  87.            {   memcpy(bp,xnew[xx],xlen[xx]) ;
  88.            ++knt ; bp += (xlen[xx]-1)  ; break ; 
  89.            }
  90.     if(was<knt) 
  91.     {   seeklog(hand, offlog ) ; 
  92.         was = write(hand, buf, buflen) ;  
  93.         if(was != buflen) serfii("%d=write(%d)", was, buflen) ; 
  94.     }     
  95.  }  
  96.  close(hand) ; 
  97.  fprintf(msgfil,"Did %3d %-12s <= %-12s IN %-12s \n", knt,xnew[0],xold[0], filna) ; 
  98. }
  99. /*******************************/
  100. STRN dacopy(STRN ins)
  101. {
  102.  char *pold = dap ;   
  103.  while( *dap++ = *ins++) ; 
  104.  return(pold) ; 
  105. }
  106. /*******************************/
  107. char *dacopz(STRN ins)
  108. {
  109.  char *pold = dap ; 
  110.  while( *dap++ = *ins++) *dap++ = 0 ; 
  111.  return(pold) ; 
  112. }
  113. /*************************/
  114. static void logger(STRN new, STRN old, int len)
  115. {
  116.  xnew[endix] = dacopy(new) ; 
  117.  xold[endix] = dacopy(old) ; 
  118.  xlen[endix++] = len ; 
  119.  
  120.  xnew[endix] = dacopz(new) ; 
  121.  xold[endix] = dacopz(old) ; 
  122.  xlen[endix++] = (2*len)-1 ; 
  123. }
  124. /*******************************/
  125. STRN delspace(STRN ins)
  126. {
  127.  char *pold = dap ;  char cc ; 
  128.  while( *dap++ = cc = *ins++) if(' '==cc) --dap ; 
  129.  return(pold) ; 
  130. }
  131. /*******************************/
  132. int main(int argc, char *argv[])
  133. {
  134.  char inp[340] ;  char sep[8] ;  char outna[40] ; 
  135.  int len1, len2 ; 
  136.  STRN inew, iold, ifil ; 
  137.  
  138.  if(2 != argc) patdoc() ; 
  139.  inpfil = jopen(argv[1],"r") ; 
  140.  strncpy(outna, argv[1], strcspn(argv[1],".")) ; 
  141.  strcat(outna, ".FX2") ; 
  142.  msgfil = jopen(outna, "w") ;
  143.  
  144.  while(NULL != fgets(inp,330,inpfil))
  145.  {  if ( 12 > strlen(inp)) continue ; 
  146.     endix = 0 ;  dap = data ; 
  147.     sep[0] = inp[0] ; sep[1] = '\n' ; sep[2] = 0 ; 
  148.     inew = strtok(inp+1, sep) ;
  149.     iold = strtok(NULL , sep) ;
  150.     len1 = strlen(iold) ; 
  151.     if(len1 != strlen(inew)) 
  152.     {   fprintf(msgfil," Error unequal len %s %s \n",iold,inew) ; 
  153.         continue ;  
  154.     }
  155.     logger(inew, iold, len1) ;   
  156.     len2 = strlen(iold =  delspace(iold)) ; 
  157.     if(len1 != len2) logger( delspace(inew) ,iold, len2) ; 
  158.     while(NULL != (ifil = strtok(NULL , sep)))   repx(ifil) ;  
  159.  }
  160.  fclose(inpfil) ; fclose(msgfil) ; 
  161.  exit(0) ; 
  162. }
  163.